package com.bawei.module_goods.util

import android.content.Context

object SearchHistoryManager {
    private const val PREF_NAME = "search_history"
    private const val KEY_HISTORY = "history_list"

    fun saveHistory(context: Context, keyword: String) {
        val list = getHistory(context).toMutableList()
        list.remove(keyword)
        list.add(0, keyword)
        if (list.size > 10) list.removeLast()
        val sp = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
        sp.edit().putString(KEY_HISTORY, list.joinToString("|||")).apply()
    }

    fun getHistory(context: Context): List<String> {
        val sp = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
        val str = sp.getString(KEY_HISTORY, "") ?: ""
        return if (str.isEmpty()) emptyList() else str.split("|||")
    }

    fun clearHistory(context: Context) {
        val sp = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
        sp.edit().remove(KEY_HISTORY).apply()
    }
} 